﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RCREEE.Library.Entities;
using System.Collections.Specialized;
using System.Configuration;
using RCREEE.Web.Classes;
using System.Threading;
using System.Globalization;
using System.IO;
using System.Data;

namespace RCREEE.Web
{
    public partial class Site : System.Web.UI.MasterPage
    {
        public User User
        {
            get
            {
                if (Session[SessionStrings.SS_User] != null)
                    return (User)Session[SessionStrings.SS_User];

                return new User();
            }
            set
            {
                Session[SessionStrings.SS_User] = value;

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                NameValueCollection attributes = new NameValueCollection();

                //TODO: Remove Force Assigning Administrator privilege
                //Session["IsAdmin"] = true;
                if (!Page.IsPostBack)
                {
                    if (Session["LanguageCode"] == null || Session["BrowsingMode"] == null)
                    {
                        // If LangugaeCode And Browing mode aren't assigned
                        // Force Assign English
                        Session["LanguageCode"] = 1033;
                        // Force Assign Categories Mode
                        Session["BrowsingMode"] = 2;
                    }
                }

                if (Session["LanguageCode"] == null || Session["BrowsingMode"] == null)
                {
                    Page.RegisterClientScriptBlock("SessionEndAlert", "<script type:text/javascript>alert('234');</script>");
                    Response.Redirect("Default.aspx");
                }

                HandelLoginButtons();

                object LanguageCode = Session["LanguageCode"];
                object BrowsingMode = Session["BrowsingMode"];
                object IsAdmin = Session["IsAdmin"];

                if (LanguageCode != null)
                {
                    attributes.Add("LanguageCode", LanguageCode.ToString());
                    attributes.Add("connectionStringName", "ConnectionString");
                    attributes.Add("BrowsingMode", BrowsingMode.ToString());

                    ChangeUICulture();

                    //SiteMapDataSource1.Provider = new SqlSiteMapProvider();

                    //SiteMapDataSource1.Provider.Initialize("SqlSiteMapProvider", attributes);
                }

                if (IsAdmin != null)
                {
                    pnlAdminLinks.Visible = Convert.ToBoolean(IsAdmin);
                    pnlCriticalDataEntry.Visible = Convert.ToBoolean(IsAdmin);
                }
                else
                {
                    pnlAdminLinks.Visible = false;
                    pnlCriticalDataEntry.Visible = false;
                }

                //switch (Convert.ToInt32(BrowsingMode))
                //{
                //    case 1:
                //        lnkIndexMode.Enabled = false;
                //        lnkEntitiesMode.Enabled = true;
                //        break;
                //    case 2:
                //        lnkIndexMode.Enabled = true;
                //        lnkEntitiesMode.Enabled = false;
                //        break;
                //    default:
                //        break;
                //}

                switch (Convert.ToInt32(LanguageCode))
                {
                    case 1025:
                        lnkEnglish.Enabled = true;
                        lnkArabic.Enabled = false;
                        lnkFrench.Enabled = true;
                        break;
                    case 1033:
                        lnkEnglish.Enabled = false;
                        lnkArabic.Enabled = true;
                        lnkFrench.Enabled = true;
                        break;
                    case 1036:
                        lnkEnglish.Enabled = true;
                        lnkArabic.Enabled = true;
                        lnkFrench.Enabled = false;
                        break;

                }
                /*
                btnHome.Text = GetLocalResourceObject("btnHomeResource1.Text").ToString();
                btnIndex.Text = GetLocalResourceObject("Button1Resource1.Text").ToString();
                btnTopics.Text = GetLocalResourceObject("Button2Resource1.Text").ToString();
                btnCountries.Text = GetLocalResourceObject("Button4Resource1.Text").ToString();
                btnAboutUs.Text = GetLocalResourceObject("Button5Resource1.Text").ToString();
                btnContactUs.Text = GetLocalResourceObject("Button6Resource1.Text").ToString();
                */
                // Load the Images from the Images/Flags Folder directly.
                DataTable dtImages = LibraryHelper.GetAllImagePathes();
                rptFooterImages.DataSource = dtImages;
                rptFooterImages.DataBind();
            }
            catch (Exception exc)
            { Response.Redirect("default.aspx"); }
        }

        private void HandelLoginButtons()
        {
            if (User != null && User.UserId > 0 &&
                Session[SessionStrings.SS_UserId] != null && Session[SessionStrings.SS_IsAdmin] != null && Session[SessionStrings.SS_Permissions] != null)
            {

                pnlLoggedIn.Visible = btnLogOut.Visible = lblUserName.Visible = true;
                //btnLogIn.Visible = false;
                lblUserName.Text = User.UserName;
            }
            else
            {
                pnlLoggedIn.Visible = btnLogOut.Visible = lblUserName.Visible = false;
                //btnLogIn.Visible = true;
            }
        }

        public string GetPageDirection()
        {
            try
            {

                if (Session["LanguageCode"] != null)
                    if (Session["LanguageCode"].ToString() == "1025")
                        return "rtl";
            }
            catch { return "ltr"; }

            return "ltr";
        }

        public bool IsAdmin = false;

        protected bool GetIsAdmin()
        {
            return false;
        }

        private void SetSessionKeyAndRefresh(string SessionKeyName, string Value)
        {
            try
            {
                Session[SessionKeyName] = Value;
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception exc)
            {

            }
        }

        private void SetSessionKeyAndRefresh(string SessionKeyName, int Value)
        {
            try
            {
                Session[SessionKeyName] = Value;
                Response.Redirect(Request.UrlReferrer.ToString());
            }
            catch (Exception exc)
            {

            }
        }

        private void ChangeUICulture()
        {
            try
            {
                string culture = "en";

                if (Session["LanguageCode"] != null)
                {
                    int LanguageCode = Convert.ToInt32(Session["LanguageCode"]);

                    switch (LanguageCode)
                    {
                        case 1033:
                            culture = "en";
                            break;

                        case 1025:
                            culture = "ar";
                            break;

                        case 1036:
                            culture = "fr";
                            break;

                        default:
                            culture = "en";
                            break;
                    }
                }

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
            }
            catch { }
        }

        protected void lnkIndexMode_Click(object sender, EventArgs e)
        { SetSessionKeyAndRefresh("BrowsingMode", 1); }

        protected void lnkEntitiesMode_Click(object sender, EventArgs e)
        { SetSessionKeyAndRefresh("BrowsingMode", 2); }

        protected void lnkEnglish_Click(object sender, EventArgs e)
        {
            Session["LanguageCode"] = 1033;
            ChangeUICulture();
            Response.Redirect(Request.UrlReferrer.ToString());
        }

        protected void lnkArabic_Click(object sender, EventArgs e)
        {
            Session["LanguageCode"] = 1025;
            ChangeUICulture();
            Response.Redirect(Request.UrlReferrer.ToString());


        }

        protected void lnkFrench_Click(object sender, EventArgs e)
        {
            Session["LanguageCode"] = 1036;
            ChangeUICulture();
            Response.Redirect(Request.UrlReferrer.ToString());
        }

        protected void BtnLogIn_Clicked(object sender, EventArgs e)
        {
            //Response.Redirect(string.Format("Login.aspx?{0}='{1}'", QueryStrings.QS_ReturnURL, Request.Url.AbsoluteUri));
            Response.Redirect(string.Format("Login.aspx?{0}={1}", QueryStrings.QS_ReturnURL,
                Request.UrlReferrer));
            //Server.UrlEncode(Request.AppRelativeCurrentExecutionFilePath + "?" + Request.QueryString)));
            // Request.AppRelativeCurrentExecutionFilePath + "?" + Request.QueryString
        }

        protected void BtnLogOut_Clicked(object sender, EventArgs e)
        {
            User = null;
            Session[SessionStrings.SS_UserId] =
            Session[SessionStrings.SS_IsAdmin] =
            Session[SessionStrings.SS_Permissions] = null;

            HandelLoginButtons();

            //Response.Redirect(string.Format("Setup.aspx?{0}='{1}'", QueryStrings.QS_ReturnURL, Request.Url.AbsoluteUri));
            //Response.Redirect(string.Format("Setup.aspx?{0}={1}", QueryStrings.QS_ReturnURL,
            //     Request.UrlReferrer));
            Response.Redirect("Default.aspx");
            //Server.UrlEncode(Request.AppRelativeCurrentExecutionFilePath + "?" + Request.QueryString)));
        }
    }
}
